package com.shop.autoshop.pay.wxpay;

import lombok.Data;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.Serializable;
import java.io.StringReader;

import static javax.xml.bind.JAXBContext.newInstance;

/**
 * @author lyt
 * @version 1.0.0
 * @ClassName AliReturnPayBean.java
 * @Description TODO
 * @createTime 2022年07月27日 14:56:00
 */
@XmlRootElement(name="xml")
@XmlAccessorType(XmlAccessType.FIELD)
@Data
public class WxpayNotifyBean implements Serializable {

    /**
     * <xml><appid><![CDATA[wx7ef6767d048f7c8f]]></appid>
     * <attach><![CDATA[1]]></attach>
     * <bank_type><![CDATA[OTHERS]]></bank_type>
     * <cash_fee><![CDATA[1]]></cash_fee>
     * <fee_type><![CDATA[CNY]]></fee_type>
     * <is_subscribe><![CDATA[Y]]></is_subscribe>
     * <mch_id><![CDATA[1600936675]]></mch_id>
     * <nonce_str><![CDATA[1660632261562]]></nonce_str>
     * <openid><![CDATA[oKJ0Z550mqjeXWXexttXCtYd4_yA]]></openid>
     * <out_trade_no><![CDATA[2022081513550921394758644817119]]></out_trade_no>
     * <result_code><![CDATA[SUCCESS]]></result_code>
     * <return_code><![CDATA[SUCCESS]]></return_code>
     * <sign><![CDATA[15AC3D45936EB70B43E37CA0751194CD]]></sign>
     * <time_end><![CDATA[20220816144450]]></time_end>
     * <total_fee>1</total_fee>
     * <trade_type><![CDATA[NATIVE]]></trade_type>
     * <transaction_id><![CDATA[4200001559202208165232324774]]></transaction_id>
     * </xml>
     */


    private String appid;
    private String attach;
    private String bank_type;
    private String cash_fee;
    private String fee_type;
    private String is_subscribe;
    private String mch_id;
    private String nonce_str;
    private String openid;
    private String out_trade_no;
    private String result_code;
    private String return_code;
    private String sign;
    private String time_end;
    private String total_fee;
    private String trade_type;
    private String transaction_id;


    /**
     *
     * @param xml
     * @return
     * @throws JAXBException
     */
    public static WxpayNotifyBean xmlToBean(String xml) throws JAXBException {
        //获得 JAXBContext 类的新实例。参数为类的地址
        JAXBContext context = newInstance(WxpayNotifyBean.class);
        //创建一个可以用来将 XML 数据转换为 java 内容树的 Unmarshaller 对象。
        Unmarshaller um = context.createUnmarshaller();
        //创建一个StringReader将xml报文转成流
        StringReader sr = new StringReader(xml);
        //调用unmarshal进行转换，并把Object类型强转为调用者的类型
        WxpayNotifyBean wxpayNotifyBean = (WxpayNotifyBean) um.unmarshal(sr);
        //将对象返回给调用者
        return wxpayNotifyBean;
    }


    public static void main(String[] args) throws JAXBException {

        String xml = "<xml><appid><![CDATA[wx7ef6767d048f7c8f]]></appid>\n" +
                "<attach><![CDATA[1]]></attach>\n" +
                "<bank_type><![CDATA[OTHERS]]></bank_type>\n" +
                "<cash_fee><![CDATA[1]]></cash_fee>\n" +
                "<fee_type><![CDATA[CNY]]></fee_type>\n" +
                "<is_subscribe><![CDATA[Y]]></is_subscribe>\n" +
                "<mch_id><![CDATA[1600936675]]></mch_id>\n" +
                "<nonce_str><![CDATA[1660632261562]]></nonce_str>\n" +
                "<openid><![CDATA[oKJ0Z550mqjeXWXexttXCtYd4_yA]]></openid>\n" +
                "<out_trade_no><![CDATA[2022081513550921394758644817119]]></out_trade_no>\n" +
                "<result_code><![CDATA[SUCCESS]]></result_code>\n" +
                "<return_code><![CDATA[SUCCESS]]></return_code>\n" +
                "<sign><![CDATA[15AC3D45936EB70B43E37CA0751194CD]]></sign>\n" +
                "<time_end><![CDATA[20220816144450]]></time_end>\n" +
                "<total_fee>1</total_fee>\n" +
                "<trade_type><![CDATA[NATIVE]]></trade_type>\n" +
                "<transaction_id><![CDATA[4200001559202208165232324774]]></transaction_id>\n" +
                "</xml>";


        WxpayNotifyBean wxpayNotifyBean = xmlToBean(xml);

        System.out.println(wxpayNotifyBean.toString());

    }
}
